﻿using System;
using System.Collections;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DBA.Common;
using DBA.Data;
using DBA.Data.ADO;
using DBA.Model;

namespace DBA.Web.SystemManage.Permissions
{
    public partial class RoleUserList : System.Web.UI.Page
    {
        public class TreeNode
        {
            public string id;
            public string name;
            public string pId;
            public bool nocheck;
            public bool open;
            public string unitType;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GetRoleTree();
            }
        }
        /// <summary>
        /// 点击查询按钮查询相关信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lnkSearch_Click(object sender, EventArgs e)
        {
            ViewState["vsUserName"] = txtName.Text.Trim();
            //ViewState["vsUserCardNum"] = txtUserCardNum.Text.Trim();
            BindData(1);
        }

        DataAccessLibrary dal = CommonQuery.DataAccessLibrary;

        protected void GetRoleTree()
        {
            List<Sys_RoleInfo> roleList = GetAllRoleList();

            List<TreeNode> nodeList = new List<TreeNode>();
            TreeNode tdRoot = new TreeNode();
            tdRoot.id = "0";
            tdRoot.name = "角色分类";
            tdRoot.nocheck = true;
            tdRoot.open = true;
            tdRoot.pId = "-1";
            nodeList.Add(tdRoot);

            foreach (Sys_RoleInfo role in roleList)
            {
                TreeNode node = new TreeNode();
                node.id = role.RoleID.ToString();
                node.name = role.RoleName;
                node.nocheck = false;
                node.open = false;
                node.pId = "0";
                nodeList.Add(node);
            }
            hidTreeData.Value = Newtonsoft.Json.JsonConvert.SerializeObject(nodeList);
        }

        /// <summary>
        /// 返回所有角色List对象
        /// </summary>
        /// <returns></returns>
        private List<Sys_RoleInfo> GetAllRoleList()
        {
            List<Sys_RoleInfo> roleList = new List<Sys_RoleInfo>();

            string strSelect = "SELECT * FROM Sys_RoleInfo";
            DataTable dt = dal.ExecuteFillDataTable(strSelect);
            foreach (DataRow dr in dt.Rows)
            {
                Sys_RoleInfo role = new Sys_RoleInfo();
                role.RoleID = ConvertHelper.ToInt32(dr["RoleID"]);
                role.RoleName = dr["RoleName"].ToString();
                role.RoleRemark = dr["RoleRemark"].ToString();
                roleList.Add(role);
            }
            return roleList;
        }


        /// <summary>
        /// 点击查询按钮查询相关信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lnkDelete_Click(object sender, EventArgs e)
        {
            if (!VerificationHelper.IsNullOrEmpty<string>(hidDeleString.Value))
            {
                DataAccessLibrary dal = CommonQuery.DataAccessLibrary;
                string value = hidDeleString.Value;
                IList<AdoCondition> delCollections = new List<AdoCondition>();
                delCollections.Add(new AdoCondition(CompareOper.IN, LogicOper.and, "RoleUserID", value));
                int reValue = dal.ModifyData.Delete<Sys_RoleAndUser>(delCollections);
                if (reValue == 1)
                {
                    BindData(1);
                    ScriptManager.RegisterStartupScript(this, base.GetType(), "deleOk" + System.Guid.NewGuid().ToString(), "parent.alertMsg.correct('数据删除成功');", true);
                }
                else
                {
                    ScriptManager.RegisterStartupScript(this, base.GetType(), "deleOk" + System.Guid.NewGuid().ToString(), "parent.alertMsg.error('数据删除失败');", true);
                }
            }
        }

        protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            BindData(e.NewPageIndex);
        }
        

        /// <summary>
        /// 得到角色用户列表
        /// </summary>
        /// <param name="ht">用来装载查询条件</param>
        /// <param name="pageSize">页尺寸</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="reCount">页总数</param>
        /// <returns></returns>
        public DataTable GetRoleGroupUsersList(Hashtable ht, int pageSize, int pageIndex, ref int reCount)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("(select distinct a.Name as EmployeeName,a.EmployeeID,a.sex,c.name as DepartmentName,b.RoleUserID")
               .Append(" from EmployeeInfo a inner join Sys_RoleAndUser b on a.EmployeeID = b.EmployeeID ")
               .Append(" left join sys_department c on a.dpt_id = c.dpt_id where b.RoleID = '" + ht["vsRoleUserID"].ToString() + "' ");

            if (ht.ContainsKey("vsUserName"))
            {
                sql.Append(" AND a.UserName like '%" + ht["vsUserName"].ToString() + "%'");
            }
            if (ht.ContainsKey("vsUserCardNum"))
            {
                sql.Append(" AND a.UserCardNum like '%" + ht["vsUserCardNum"].ToString() + "%'");
            }
            sql.Append(")");

            return dal.ModifyData.GetPageDataTable(sql.ToString(), "*", "", "RoleUserID DESC", pageIndex, pageSize, ManagerSQL.PageViewTableType.MixtureTable, ref reCount);
        }

        private void BindData(int pageIdx)
        {
            if (!String.IsNullOrEmpty(hidRoleID.Value))
            {
                int reCount = 0;
                Hashtable ht = new Hashtable();
                ht.Add("vsRoleUserID", hidRoleID.Value);

                if (ViewState["vsUserName"] != null && !String.IsNullOrEmpty(ViewState["vsUserName"].ToString()))
                {
                    ht.Add("vsUserName", ViewState["vsUserName"].ToString());
                }
                if (ViewState["vsUserCardNum"] != null && !String.IsNullOrEmpty(ViewState["vsUserCardNum"].ToString()))
                {
                    ht.Add("vsUserCardNum", ViewState["vsUserCardNum"].ToString());
                }

                replData.DataSource = GetRoleGroupUsersList(ht, AspNetPager1.PageSize, pageIdx, ref reCount);
                replData.DataBind();
                AspNetPager1.RecordCount = reCount;
            }
        }
    }

}